package com.eqdd.yiqidian.map;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectMapActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.ArrayList;

import rx.Subscription;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class ShowMapActivity extends BaseActivity implements LocationSource, AMapLocationListener {
    private SelectMapActivityCustom dataBinding;
    private BitmapDescriptor mBitmapDescriptor;
    private AMap mAMap;
    private Marker mMarker;
    private LocationSource.OnLocationChangedListener mLocationChangedListener;
    private AMapLocationClient mlocationClient;
    private double lat;
    private double lon;
    private String areaContent;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_map);
        initTopTitleBar(View.VISIBLE, "位置");
    }

    @Override
    public void initData() {
        lat = getIntent().getDoubleExtra(Config.LAT, 0);
        lon = getIntent().getDoubleExtra(Config.LON, 0);
        areaContent = getIntent().getStringExtra(Config.AREA_CONTENT);
        RxPermissions.getInstance(this)
                .request(ACCESS_FINE_LOCATION)
                .subscribe(granted -> {
                    if (granted) {
                        initMap();
                    } else {
                        ToastUtil.showShort("请开启定位权限");
                        finish();
                    }
                });
    }

    private void setMaker(LatLng latLng, String address) {
        if (mMarker == null) {
            mBitmapDescriptor = BitmapDescriptorFactory.fromResource(io.rong.imkit.R.drawable.rc_ext_location_marker);
            MarkerOptions markerOptions = (new MarkerOptions()).position(latLng).icon(this.mBitmapDescriptor);
            mMarker = mAMap.addMarker(markerOptions);
        }
        mMarker.setPosition(latLng);
        if (!TextUtils.isEmpty(address)) {
            dataBinding.tvAddress.setText(address);
        }

    }

    private void initMap() {
        mAMap = dataBinding.mapView.getMap();
        dataBinding.mapView.onCreate(getSavedInstanceState());
        mAMap.setLocationSource(this);
        mAMap.setMyLocationEnabled(true);
        mAMap.getUiSettings().setZoomControlsEnabled(false);
        mAMap.getUiSettings().setMyLocationButtonEnabled(true);
        mAMap.setMapType(1);

        if (!TextUtils.isEmpty(areaContent)) {
            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
                    new LatLng(lat, lon), 15);
            mAMap.moveCamera(update);
            setMaker(new LatLng(lat, lon), areaContent);
        } else {
            MyLocationStyle myLocationStyle = new MyLocationStyle();
            myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(io.rong.imkit.R.drawable.rc_ext_my_locator));
            myLocationStyle.strokeWidth(0.0F);
            myLocationStyle.strokeColor(io.rong.imkit.R.color.rc_main_theme);
            myLocationStyle.radiusFillColor(0);
            mAMap.setMyLocationStyle(myLocationStyle);
        }
    }

    private void initLocation() {
        if (mlocationClient == null) {
            AMapLocationClientOption mLocationOption;
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            mlocationClient.setLocationListener(this);
            //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            mLocationOption.setMockEnable(true);//是否允许模拟
            mLocationOption.setOnceLocationLatest(true);
            mlocationClient.setLocationOption(mLocationOption);
        }
        if (TextUtils.isEmpty(areaContent)) {
            dataBinding.tvAddress.setText(null);
            mlocationClient.startLocation();
        }


    }


    public void activate(OnLocationChangedListener onLocationChangedListener) {
        this.mLocationChangedListener = onLocationChangedListener;
        initLocation();
    }

    public void deactivate() {
    }

    protected void onDestroy() {
        dataBinding.mapView.onDestroy();
        if (mlocationClient != null) {
            mlocationClient.onDestroy();
        }
        super.onDestroy();
    }


    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }


    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        System.out.println("当前类=SelectMapActivity.onLocationChanged()");
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                mLocationChangedListener.onLocationChanged(amapLocation);// 显示系统小蓝点
                dataBinding.tvAddress.setText(amapLocation.getAddress());
            } else {
                ToastUtil.showShort("定位失败");
            }
        }
    }
}
